/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.sdRankingSys;

import com.SySammy.sdRankingSys.Handlers.PerksHandler;
import com.SySammy.sdRankingSys.Handlers.VocationHandler;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class sdRankCmds implements CommandExecutor {

    private sdRS plugin;
    VocationHandler VH;
    PerksHandler PH;

    public sdRankCmds(sdRS aThis) {
        this.plugin = aThis;
    }

    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        String[] Nargs = strings;
        String cmd = cmnd.getName().toLowerCase();
        if (cmd.equals("vocation")) {
            return RankMethod(cs, Nargs);
        } else if (cmd.equals("perks")) {
            return PerksMethod(cs, Nargs);
        }
        return true;
    }

    private boolean RankMethod(CommandSender cs, String[] args) {
        VH = new VocationHandler(plugin);
        Player ply = (Player) cs;
        try {
            if (args.length <= 0) {
                if (!plugin.canUseVocationHelp(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                ply.sendMessage(ChatColor.DARK_AQUA + "Your current stats are:");
                ply.sendMessage(ChatColor.DARK_AQUA + "Vocation: " + ChatColor.GOLD + VH.PlyVocation(ply));
                ply.sendMessage(ChatColor.DARK_AQUA + "Level: " + ChatColor.GOLD + VH.PlyVocationLvl(ply));
                ply.sendMessage(ChatColor.DARK_AQUA + "Exp: " + ChatColor.GOLD + VH.GetExp(ply) + "/" + VH.LvlUpExp(ply));
                return true;
            } else if (args[0].equalsIgnoreCase("-j") && args.length >= 2) {
                if (!plugin.canUseJoinVocation(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                String vocation = args[1];
                if (VH.HasVocation(ply)) {
                    ply.sendMessage(ChatColor.RED + "You need to leave your current vocation to join another.");
                    return true;
                } else {
                    VH.RegVocation(ply, vocation);
                }
                return true;
            } else if (args[0].equalsIgnoreCase("-l") && args.length >= 1) {
                if (!plugin.canUseVocationList(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                ply.sendMessage(ChatColor.DARK_AQUA + "List of current available Vocations: ");
                for (int x = 1; x < VH.VocationList().size(); x++) {
                    ply.sendMessage(ChatColor.GOLD + VH.VocationList().get(x).toUpperCase());
                }
                return true;
            } else if (args[0].equalsIgnoreCase("-e") && args.length >= 3) {
                if (!ply.isOp()) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                Player p = plugin.getServer().getPlayer(args[1]);
                int exp = Integer.parseInt(args[2]);
                if (p != null) {
                    VH.AddExp(p, exp);
                }
                return true;
            } else if (args[0].equalsIgnoreCase("-h") && args.length >= 1) {
                if (!plugin.canUseVocationHelp(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                return false;
            }
            return false;
        } catch (Exception e) {
            cs.sendMessage(ChatColor.RED + "Input error, please try again.");
            return false;
        }
    }

    private boolean PerksMethod(CommandSender cs, String[] args) {
        PH = new PerksHandler(plugin);
        Player ply = (Player) cs;
        try {
            if (args.length <= 0) {
                if (!plugin.canUsePerksStatus(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                ply.sendMessage(ChatColor.DARK_AQUA + "Your current perks are:");
                for (int x = 0; x
                        < PH.NumbOfPerks(ply); x++) {
                    ply.sendMessage(ChatColor.DARK_AQUA + "Slot " + String.valueOf(x + 1) + ": " + ChatColor.GOLD + PH.PerkInfo(ply, x + 1));
                }
                return true;
            } else if (args[0].equalsIgnoreCase("-l") && args.length >= 1) {
                if (!plugin.canUsePerksList(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                PH.ListOfPerks(ply);
                return true;
            } else if (args[0].equalsIgnoreCase("-a") && args.length >= 3) {
                if (!plugin.canUsePerksAdd(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                PH.RegPerk(ply, Integer.parseInt(args[1]), args[2].toLowerCase());
                return true;
            } else if (args[0].equalsIgnoreCase("-r") && args.length >= 2) {
                if (!plugin.canUsePerksRemove(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                PH.RemovePerk(ply, Integer.parseInt(args[1]));
                return true;
            } else if (args[0].equalsIgnoreCase("-h") && args.length >= 2) {
                if (!plugin.canUsePerksHelp(ply)) {
                    cs.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                    return true;
                }
                return false;
            }
            return false;
        } catch (Exception e) {
            cs.sendMessage(ChatColor.RED + "Input error, please try again.");
            return false;
        }
    }
}
